<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test case for save devtools settings</title>
</head>
<body>
  <p id="wait" style="font-size:1.5em">Please wait to be closed.</p>
  <input type="text" id="color" placeholder="css color"/>
  <script>
    var gui = require('nw.gui');
    var win = gui.Window.get();
    var result = new Array();
    //action: 1 change the devtools settings
    var action = gui.App.argv[0];

    //load devtools into iframe
    win.showDevTools('', true);
    win.on("devtools-opened", function(url) {
        var iframe_devtools = document.createElement("iframe");
	iframe_devtools.id = 'devtools';
	iframe_devtools.src = url;
	iframe_devtools.height = '1000';
	iframe_devtools.width = '1000';
	function change_setttings() {
	    var scripts = devtools.document.getElementsByClassName('scripts');

	    // make sure it is complete 
	    if(scripts == undefined  || scripts[0] == undefined || scripts[0].click == undefined)
                setTimeout(change_setttings, 3000);
	    else {
	    	// open settitng 
		devtools.WebInspector.SettingsController.prototype.showSettingsScreen();
		// save original settings
		// general
		devtools.WebInspector.settingsController._settingsScreen._tabbedPane._tabs[0]._tabbedPane.selectTab('general', true);
		result.push(devtools.document.getElementsByName('Disable cache (while DevTools is open)')[0].checked);
		result.push(devtools.document.getElementsByName('Disable JavaScript')[0].checked);
		result.push(devtools.document.getElementsByName('Split panels vertically when docked to right')[0].checked);
		// 0~3
		for (var i = 0; i < 4; i++) 
		    if (devtools.document.getElementsByClassName('toplevel')[0].childNodes[1][i]) {
			result.push(i);
			break;
		    }
		result.push(devtools.document.getElementsByName('Show user agent styles')[0].checked);
		result.push(devtools.document.getElementsByName('Word wrap')[0].checked);
		result.push(devtools.document.getElementsByName('Show Shadow DOM')[0].checked);
		result.push(devtools.document.getElementsByName('Show rulers')[0].checked);
		result.push(devtools.document.getElementsByName('Show paint rectangles')[0].checked);
		result.push(devtools.document.getElementsByName('Show composited layer borders')[0].checked);
		result.push(devtools.document.getElementsByName('Search in content scripts')[0].checked);
		result.push(devtools.document.getElementsByName('Enable JS source maps')[0].checked);
		result.push(devtools.document.getElementsByName('Enable CSS source maps')[0].checked);
		// 0~3
		result.push(devtools.document.getElementsByClassName('toplevel')[0].childNodes[1].selectedIndex);
		result.push(devtools.document.getElementsByName('Show whitespace characters')[0].checked);
		result.push(devtools.document.getElementsByName("Detect indentation")[0].checked);
		result.push(devtools.document.getElementsByName('Limit number of captured JS stack frames')[0].checked);
		result.push(devtools.document.getElementsByClassName('numeric')[0].value);
		result.push(devtools.document.getElementsByName('Show CPU activity on the ruler')[0].checked);
		result.push(devtools.document.getElementsByName('Log XMLHttpRequests')[0].checked);
		result.push(devtools.document.getElementsByName('Preserve log upon navigation')[0].checked);
	        if (process.platform != 'darwin')
		        result.push(devtools.document.getElementsByName('Enable Ctrl + 1-9 shortcut to switch panels')[0].checked);
         	else
            		result.push(devtools.document.getElementsByName('Enable Cmd + 1-9 shortcut to switch panels')[0].checked);
		// overrides 
		devtools.WebInspector.settingsController._settingsScreen._tabbedPane._tabs[0]._tabbedPane.selectTab("overrides", true);

                result.push(devtools.document.getElementsByClassName('help-block')[0].childNodes[0].childNodes[0].checked);//
                result.push(devtools.document.getElementsByClassName('help-block')[0].childNodes[1].childNodes[0].childNodes[0].checked);//Enable on Devtools startup 
		// 0~21
		result.push(devtools.document.getElementsByClassName('help-block')[1].childNodes[0].childNodes[1].childNodes[0].selectedIndex);
		result.push(devtools.document.getElementsByName('User Agent')[0].checked);//Overrides User agent
		
		// ignore next
		//result.push(devtools.document.getElementById('metrics-override-checkbox').checked);
		result.push(devtools.document.getElementsByName('Device metrics')[0].checked);
		// result.push(devtools.document.getElementById('metrics-override-width').value);
		// result.push(devtools.document.getElementById('metrics-override-height').value);
		// ignore next next
		result.push(devtools.document.getElementsByName('Fit in window')[0].checked);
		result.push(devtools.document.getElementsByName('Override Geolocation')[0].checked);
		result.push(devtools.document.getElementById('geolocation-override-latitude').value);
		result.push(devtools.document.getElementById('geolocation-override-longitude').value);
		result.push(devtools.document.getElementById('geolocation-error').checked);

		result.push(devtools.document.getElementsByName('Override Device Orientation')[0].checked);

		result.push(devtools.document.getElementById('device-orientation-override-alpha').value);
		result.push(devtools.document.getElementById('device-orientation-override-beta').value);
		result.push(devtools.document.getElementById('device-orientation-override-gamma').value);
		result.push(devtools.document.getElementsByClassName('help-block')[4].childNodes[0].childNodes[0].childNodes[0].checked);
		result.push(devtools.document.getElementsByClassName('help-block')[5].childNodes[0].childNodes[0].childNodes[0].checked);
		// 0~8
		//result.push(devtools.document.getElementsByClassName('help-block')[5].childNodes[0].childNodes[1].selectedIndex);
		result.push(devtools.document.getElementsByName('Emulate CSS media')[0].checked);
		if (action == '1') {
		    var i = 0;
		    // change original settings
		    // general
		    devtools.WebInspector.settingsController._settingsScreen._tabbedPane._tabs[0]._tabbedPane.selectTab('general', true);
		    devtools.document.getElementsByName('Disable cache (while DevTools is open)')[0].click(); i++;
		    devtools.document.getElementsByName('Disable JavaScript')[0].click(); i++;
		    devtools.document.getElementsByName('Split panels vertically when docked to right')[0].click(); i++;
		    // 0~3
		    devtools.document.getElementsByClassName('toplevel')[0].childNodes[1][(result[i++] + 1) % 4].click();

		    devtools.document.getElementsByName('Show user agent styles')[0].click(); i++;
		    devtools.document.getElementsByName('Word wrap')[0].click(); i++;
		    devtools.document.getElementsByName('Show Shadow DOM')[0].click(); i++;
		    devtools.document.getElementsByName('Show rulers')[0].click(); i++;
		    devtools.document.getElementsByName('Show paint rectangles')[0].click(); i++;
		    devtools.document.getElementsByName('Show composited layer borders')[0].click(); i++;
		    devtools.document.getElementsByName('Search in content scripts')[0].click(); i++;
		    devtools.document.getElementsByName('Enable JS source maps')[0].click(); i++;
		    devtools.document.getElementsByName('Enable CSS source maps')[0].click(); i++;

		    if (!result[i - 1])
			// 0~3
			devtools.document.getElementsByClassName('toplevel')[0].childNodes[1].selectedIndex = (result[i++] + 1) % 4;
		    devtools.document.getElementsByName('Show whitespace characters')[0].click(); i++;
		    devtools.document.getElementsByName("Detect indentation")[0].click(); i++;
		    devtools.document.getElementsByName('Limit number of captured JS stack frames')[0].click(); i++;
		    if (!result[i - 1])
			devtools.document.getElementsByClassName('numeric')[0].value = (result[i++] + 1) % 10;
		    devtools.document.getElementsByName('Show CPU activity on the ruler')[0].click(); i++;
		    devtools.document.getElementsByName('Log XMLHttpRequests')[0].click(); i++;
		    devtools.document.getElementsByName('Preserve log upon navigation')[0].click(); i++;
                    if (process.platform != 'darwin')
		        devtools.document.getElementsByName('Enable Ctrl + 1-9 shortcut to switch panels')[0].click();
		    else
            		devtools.document.getElementsByName('Enable Cmd + 1-9 shortcut to switch panels')[0].click();
		    i++;

		    // overrides 
		    devtools.WebInspector.settingsController._settingsScreen._tabbedPane._tabs[0]._tabbedPane.selectTab("overrides", true);
                    devtools.document.getElementsByClassName('help-block')[0].childNodes[0].childNodes[0].click(); i++;//
                    devtools.document.getElementsByClassName('help-block')[0].childNodes[1].childNodes[0].childNodes[0].click(); i++//Enable on Devtools startup 
		    if (!result[i - 1])
			// 0~21
			devtools.document.getElementsByClassName('help-block')[1].childNodes[0].childNodes[1].childNodes[0].selectedIndex = (result[i++] + 1) % 22;
		    // ignore next
		    //devtools.document.getElementById('metrics-override-checkbox').click(); i++;
		    devtools.document.getElementsByName('Device metrics')[0].click(); i++;
		    // result.push(devtools.document.getElementById('metrics-override-width').value);
		    // result.push(devtools.document.getElementById('metrics-override-height').value);
		    // ignore next next
		    devtools.document.getElementsByName('Fit in window')[0].click(); i++;
		    devtools.document.getElementsByName('Override Geolocation')[0].click(); i++;
		    if (!result[i - 1]) {
			devtools.document.getElementById('geolocation-override-latitude').value =  (result[i++] + 1) % 10;
			devtools.document.getElementById('geolocation-override-longitude').value = (result[i++] + 1) % 10;
		    }
		    devtools.document.getElementById('geolocation-error').click(); i++;
		    devtools.document.getElementsByName('Override Device Orientation')[0].click(); i++;
		    if (!result[i - 1]) {
			devtools.document.getElementById('device-orientation-override-alpha').value = (result[i++] + 1) % 10;
			devtools.document.getElementById('device-orientation-override-beta').value = (result[i++] + 1) % 10;
			devtools.document.getElementById('device-orientation-override-gamma').value = (result[i++] + 1) % 10;
		    }
		    devtools.document.getElementsByClassName('help-block')[4].childNodes[0].childNodes[0].childNodes[0].click(); i++;
		    devtools.document.getElementsByClassName('help-block')[5].childNodes[0].childNodes[0].childNodes[0].click(); i++;
		    devtools.document.getElementsByName('Emulate CSS media')[0].checked;
		    //if (!result[i - 1])
			// 0~8
		//	devtools.document.getElementsByClassName('help-block')[5].childNodes[0].childNodes[1].selectedIndex = (result[i++] + 1) % 9;
		}
		win.showDevTools();

		var client = require('../../nw_test_app').createClient({
                    argv: gui.App.argv,
                    data: result
		});
	    }
	}
	iframe_devtools.onload = change_setttings;
	document.getElementById("wait").appendChild(iframe_devtools);
    });
</script>
</body>
</html>
